package cn._51doit.kafka.clients;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class JavaProducer {

    public static void main(String[] args) {

        Properties properties = new Properties();
        //指定broker的地址
        properties.setProperty("bootstrap.servers", "node-1.51doit.cn:9092,node-2.51doit.cn:9092,node-3.51doit.cn:9092");
        //指定写入数据key的序列化方式
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        //指定写入数据value的序列化方式
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);

        //封装的发送数据
        ProducerRecord<String, String> record1 = new ProducerRecord<String, String>("test", "hello kafka1");

        kafkaProducer.send(record1);

        ProducerRecord<String, String> record2 = new ProducerRecord<String, String>("test", "hello kafka2");

        kafkaProducer.send(record2);

        System.out.println(6666);

        //最好在调用close之前，调用一次flush
        kafkaProducer.flush();

        //将kafkaProducer在close之前，会将缓存的数据flush到broker中
        kafkaProducer.close();

    }
}
